﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MemoryGames.Database;

namespace MemoryGames.Configuration
{
    class Settings
    {
        public static string Load(string settingType, string settingName)
        {
            ModelDataContext context = new ModelDataContext();

            var result = from s in context.Settings
                         where s.Type == settingType && s.Name == settingName
                         select s.Value;

            if(result.Count() == 0) return string.Empty;
            return result.First();
        }

        public static void Save(string settingType, string settingName, string settingValue)
        {
            ModelDataContext context = new ModelDataContext();

            //GK Get or create setting object
            var result = from s in context.Settings
                         where s.Type == settingType && s.Name == settingName
                         select s;
            Setting setting = null;
            if (result.Count() == 0)
            {
                setting = new Setting() { Type = settingType, Name = settingName };
                context.Settings.InsertOnSubmit(setting);
            }
            else
                setting = result.First();

            setting.Value = settingValue;

            //GK Save changes
            context.SubmitChanges();
        }
    }
}
